package com.hlpedu.controller;

import com.hlpedu.pojo.UserForm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user/")
public class UserController {
    private static final Log logger = LogFactory.getLog(UserController.class);

    //点击注册1跳转到toRegister.jsp页面
    @RequestMapping("goRegister")
    public String goRegister() {
        return "toRegister";
    }

    //通过@ModelAttribute接收请求参数
    /*@ModelAttribute注解用于修饰多个请求参数封装到实体对象中，从而建行数据绑定过程*/
    @RequestMapping("testModelAttr")
    @ResponseBody
    public String testModelAttr(@ModelAttribute("user") UserForm userForm) {
        logger.info(userForm.getUsername() +
                ";" + userForm.getPassword() +
                ";" + userForm.getConfirmPwd());
        String msg = "";
        //校验参数
        if (!("谜一样的男人".equals(userForm.getUsername()))
                && userForm.getPassword().equals(userForm.getConfirmPwd())) {//表示注册成功
            msg = "success";
            return msg;
        } else {//表示注册失败
            msg = "用户名已存在";
            return msg;
        }
    }

    //    通过@PathVarliable注解接收url中请求参数
    //http://localhost:8080/user/testPathVar/%E5%BC%A0%E4%B8%89
    //http://localhost:8080/user/testPathVar/zhangsan/123456
    @RequestMapping("testPathVar/{uname}/{pwd}")
    public String testPathVar(@PathVariable String uname,
                              @PathVariable String pwd,
                              Model model) {
        if (uname != null && pwd != null) {
            logger.info("接收成功uname=" + uname);
            logger.info("接收成功pwd=" + pwd);
            //向页面响应提示信息
            model.addAttribute("message", "接收成功");
            return "success";
        } else {
            //向页面响应提示信息
            model.addAttribute("message", "接收失败");
            return "success";
        }
    }

    //点击登录1，添加到logon.jsp页面
    @RequestMapping("toRequest")
    public String toRequest() {
        return "logon";
    }

    //通过HttpServletRequest接收请求参数
    @RequestMapping(value = "testRequest", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String testRequest(HttpServletRequest request) {
        //实现登录功能
        //获取参数值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //参数校验
        String msg = "";
        if ("谜一样的男人".equals(username)
                && "80231314".equals(password)) {
            msg = "ok";
            System.out.println(msg);
            return msg;
        } else {
            System.out.println(msg);
            msg = "用户名或密码错误";
            return msg;
        }
    }

    //接收注册页面表达数据，进行校验并响应结果给前端
    @RequestMapping(value = "register", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    /*@RequestParam可以为形参起别名。
    作用：当形参的参数名和前端变量名不一致时，需要使用该注解来修饰形参，否则程序报错。
    常用属性：
      value属性表示参数名；required表示是否包含该参数，默认值是true即必须包含；
      defaultValue属性表示为参数设置默认值，当设置了该属性，
      required = true会失效，自动变为false，当前参数使用该默认值*/
    public String register(
            @RequestParam(value = "username",
                    required = true,
                    defaultValue = "彷徨中微笑") String name,
            @RequestParam("password") String pwd,
            @RequestParam("confirmPwd") String cPwd) {
        //System.out.println(name+";"+);
        logger.info(name + ";" + pwd + ";" + cPwd);
        String msg = "";
        //校验参数
        if (!("谜一样的男人".equals(name)) && pwd.equals(cPwd)) {//表示注册成功
            msg = "success";
            return msg;
        } else {//表示注册失败
            msg = "用户名已存在";
            return msg;
        }
    }

    //点击登注册按钮，展示注册页面
    @RequestMapping("toRegister")
    public String toRegister() {
        return "register";
    }

    //接收表单数据，进行校验并将数据响应到页面中
//    @RequestMapping("/user/login")
    @RequestMapping(value = "login",
            produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String login(UserForm userFrom) {
        System.out.println(userFrom.getUsername());
        String msg="";
        if("谜一样的男人".equals(userFrom.getUsername()) && "80231314".equals(userFrom.getPassword())){
            msg="ok";
            System.out.println(msg);
            return msg;
        }else{
            System.out.println(msg);
            msg = "用户名或密码错误";
            return msg;
        }
    }

    //点击登录按钮，展示登录页面
//    @RequestMapping("/user/toLogin")
    @RequestMapping({ "toLogin"})
    public String toLogin() {
        return "login";
    }
}

